Construct a operator to manage the stages for another object, called "methods owner". Generally, the operator helps the methods owner, by exposing or hiding some methods for the methods owner at correct stage.
// Note that the stagesOperator is NOT the stageMethodsOwner.
var stagesOperator = new WulechuanApplyOneStageOneMethodProgrammingPatternToMethodsOwner(
stageMethodsOwner,
preferredLanguage
);
-
stageMethodsOwner [required]
The object to add or remove methods upon.
-
preferredLanguage [Optional]
The preferred language for exposing and hiding methods. This argument is optional here as constructing an instance, but the preferred language is required anyway before starting from the first stage.
var StagesOperator = require('@wulechuan/apply-one-stage-one-method-pattern');
var a = {};
var stagesOperator = new StagesOperator(a, 'en-US');
- 设有步骤
- 添加步骤
- addStage
Define a stage for a provided function. When the time(stage) is correct, the function will then be attached to the methods owner, exposed as a method of its.
Might be any of below:
function addStage(
stageAction, /* a function */
actionAliasesInAllLanguages /* an object */
);
function addStage(
stageAction, /* a function */
thisStageCanBeSkipped, /* a boolean */
actionAliasesInAllLanguages /* an object */
);
function addStage(
stageAction, /* a function */
anAliasInPreferredLanguage /* a non-empty string */
);
function addStage(
stageAction, /* a function */
thisStageCanBeSkipped, /* a boolean */
anAliasInPreferredLanguage /* a non-empty string */
);
-
stageAction [required]
Must be a function.
A function that will be added to the operator as its method at correct stage.
-
thisStageCanBeSkipped [Optional]
Must be a boolean.
A value of true means the stage being added is an optional stage, so that the method after this optional stage should also be exposed at the previous stage of this optional stage.
-
actionAliasesInAllLanguages [required]
- If the preferred language has NOT been set before the invocation of this method, then this argument must be an object.
- If the preferred language has been set before the invocation of this method, then a non-empty string is allowed, which will be treated as the alias in the preferred language. Of course, an object is allowed as always.
An object that takes several arrays, each contains aliases in a specific language.
stageOperator.addStage(aFunction, {
'en-US': 'myFirstMethod',
'zh-CN': '第一步'
});
// or
stageOperator.addStage(
anotherFunction,
true // Skippable,
'nonImportantStep'
);
// or
stageOperator.addStage(
aThirdFunction,
'youCanNotSkipMe'
);
// or
stageOperator.设有步骤(
步骤函数本体,
'我是第四个步骤' // in English, this means "I am the fourth step, or stage."
);
- 优先采用该语言
- 优先使用该语言
- setPreferredNaturalLanguageTo
Set the preferred language to use.
function setPreferredNaturalLanguageTo(languageCode);
-
languageCode
Must be a non-empty string.
stageOperator.setPreferredNaturalLanguageTo('en-US');
// another example
stageOperator.优先采用该语言('zh-CN');
- 从头开始
- 开始
- startFromFirstStage
- start
Start the execution chain on the methods owner.
function startFromFirstStage();
None.
stageOperator.startFromFirstStage();
// another example
stageOperator.从头开始();
- 中止
- 中断
- 停止
- abort
- stop
Stop the execution chain on the methods owner. If this method of the operator is invoked, all methods for all subsequence stages of the owner will be skipped. And the finally returned value of the last stage is always undefined.
-
Tip:
This method of the operator is often, though not forced to be, invoked withing a method of the methods owner, which make sense.
function abort();
None.
stageOperator.abort();
// another example
stageOperator.中止();