move-by-move 1.0.0 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.d.ts +9 -9
- package/lib/index.js +8 -6
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
interface GameAction {
|
|
1
|
+
export interface GameAction {
|
|
2
2
|
type: string;
|
|
3
3
|
}
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export
|
|
4
|
+
export interface Game<Type> {
|
|
5
|
+
subscribe(listener: Function): void;
|
|
6
|
+
unsubscribe(listener: Function): void;
|
|
7
|
+
getState(): Type;
|
|
8
|
+
move(action: GameAction): void;
|
|
9
|
+
}
|
|
10
|
+
export declare type RuleIO<Type> = [action: GameAction, state: Type];
|
|
11
|
+
export declare function createGame<Type>(rules: Array<(input: RuleIO<Type>) => RuleIO<Type>>, initialGameState: Type): Game<Type>;
|
package/lib/index.js
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createGame = void 0;
|
|
3
4
|
const helpers_1 = require("./helpers");
|
|
4
|
-
|
|
5
|
+
function createGame(rules, initialGameState) {
|
|
5
6
|
let listeners = [];
|
|
6
7
|
let state = initialGameState;
|
|
7
8
|
const subscribe = (listener) => {
|
|
8
9
|
listeners = [...listeners, listener];
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
};
|
|
11
|
+
const unsubscribe = (listener) => {
|
|
12
|
+
listeners = listeners.filter(item => item !== listener);
|
|
12
13
|
};
|
|
13
14
|
const getState = () => {
|
|
14
15
|
return state;
|
|
@@ -22,8 +23,9 @@ const createGame = (rules, initialGameState) => {
|
|
|
22
23
|
};
|
|
23
24
|
return {
|
|
24
25
|
subscribe,
|
|
26
|
+
unsubscribe,
|
|
25
27
|
getState,
|
|
26
28
|
move,
|
|
27
29
|
};
|
|
28
|
-
}
|
|
29
|
-
exports.
|
|
30
|
+
}
|
|
31
|
+
exports.createGame = createGame;
|