@dharmax/state-router 1.0.1 → 1.1.2

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/ReadMe.md CHANGED
@@ -19,7 +19,7 @@ each state can be given a logical name, route and an associated page/component.
19
19
  stateManager.addState('main', 'main-page', /main$/);
20
20
  stateManager.addState('login', 'login-box', 'login')
21
21
  stateManager.addState('signup', 'signup-box', /signup/)
22
- stateManager.addState('my-profile')
22
+ stateManager.addState('my-profile') // will assume the page name and the route are the same...
23
23
  stateManager.addState('inbox')
24
24
  stateManager.addState('about')
25
25
  stateManager.addState('discussion', 'discussion-page', 'discussion/%')
@@ -31,7 +31,7 @@ In your main component, you write something like that:
31
31
 
32
32
  ```javascript
33
33
 
34
- dispatcher.on('state:changed', event => {
34
+ stateManager.onChange( event => {
35
35
  // this specific example works with RiotJs, but you get the drift
36
36
  this.update({
37
37
  currentPage: event.data.pageName
@@ -0,0 +1,2 @@
1
+ export * from "./router";
2
+ export * from "./state-manager";
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./router";
2
+ export * from "./state-manager";
@@ -1,4 +1,5 @@
1
1
  import { RoutingMode } from "./router";
2
+ import { IPubSubHandle, PubSubEvent } from "@dharmax/pubsub";
2
3
  export declare type ApplicationStateName = string;
3
4
  export declare type ApplicationState = {
4
5
  name: ApplicationStateName;
@@ -11,7 +12,9 @@ export declare class StateManager {
11
12
  private appState;
12
13
  private previousState;
13
14
  private stateContext;
15
+ static dispatcher: import("@dharmax/pubsub").PubSub;
14
16
  constructor(mode?: RoutingMode);
17
+ onChange(handler: (event: PubSubEvent, data: any) => void): IPubSubHandle;
15
18
  getState(): ApplicationState;
16
19
  get previous(): ApplicationState;
17
20
  get context(): ApplicationState;
@@ -6,6 +6,9 @@ export class StateManager {
6
6
  router.mode = mode;
7
7
  router.listen();
8
8
  }
9
+ onChange(handler) {
10
+ return StateManager.dispatcher.on('state:changed', handler);
11
+ }
9
12
  getState() {
10
13
  return this.appState || {};
11
14
  }
@@ -83,4 +86,5 @@ export class StateManager {
83
86
  });
84
87
  }
85
88
  }
89
+ StateManager.dispatcher = dispatcher;
86
90
  export const stateManager = new StateManager();
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@dharmax/state-router",
3
- "version": "1.0.1",
3
+ "version": "1.1.2",
4
4
  "description": "A cute and tight router and application state controller",
5
- "main": "dist/router.js",
6
- "types": "dist/router.d.ts",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
7
  "scripts": {
8
8
  "test": "echo \"Error: no test specified\" && exit 1",
9
9
  "build": "npx tsc",
@@ -15,6 +15,7 @@
15
15
  },
16
16
  "keywords": [
17
17
  "router",
18
+ "navigation",
18
19
  "state"
19
20
  ],
20
21
  "author": "Avi Tshuva, dharmax",
@@ -27,6 +28,6 @@
27
28
  "typescript": "^4.4.3"
28
29
  },
29
30
  "dependencies": {
30
- "@dharmax/pubsub": "^1.0.1"
31
+ "@dharmax/pubsub": "^1.1.0"
31
32
  }
32
33
  }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./router";
2
+ export * from "./state-manager";
package/src/router.ts CHANGED
@@ -2,7 +2,7 @@ class Route {
2
2
  constructor() {
3
3
  }
4
4
 
5
- re: RegExp = null
5
+ re: RegExp = null
6
6
  handler: Function = null
7
7
  }
8
8
 
@@ -13,9 +13,9 @@ export const router = new class {
13
13
  routes: Route[] = []
14
14
  root = '/'
15
15
 
16
- baseLocation: string | null= null;
16
+ baseLocation: string | null = null;
17
17
 
18
- resetRoot ( root:string) {
18
+ resetRoot(root: string) {
19
19
  this.root = '/' + this.clearSlashes(root) + '/';
20
20
  }
21
21
 
@@ -49,7 +49,7 @@ export const router = new class {
49
49
  return `${a[0]}#${afterHash}`
50
50
  }
51
51
 
52
- add(re: Function | RegExp, handler: Function ) {
52
+ add(re: Function | RegExp, handler: Function) {
53
53
  if (typeof re == 'function') {
54
54
  handler = re;
55
55
  re = null;
@@ -58,7 +58,7 @@ export const router = new class {
58
58
  return this;
59
59
  }
60
60
 
61
- process(location?:string) {
61
+ process(location?: string) {
62
62
  const fragment = location || this.getLocation();
63
63
  const matches = this.routes.filter((r: any) => fragment.match(r.re))
64
64
 
@@ -1,5 +1,6 @@
1
1
  import {router, RoutingMode} from "./router";
2
- import dispatcher from "@dharmax/pubsub";
2
+ import dispatcher, {IPubSubHandle, PubSubEvent} from "@dharmax/pubsub";
3
+
3
4
 
4
5
  export type ApplicationStateName = string
5
6
 
@@ -17,6 +18,8 @@ export class StateManager {
17
18
  private previousState: ApplicationState
18
19
  private stateContext: ApplicationState
19
20
 
21
+ public static dispatcher = dispatcher
22
+
20
23
  constructor(mode: RoutingMode = 'hash') {
21
24
 
22
25
  router.mode = mode
@@ -24,6 +27,11 @@ export class StateManager {
24
27
 
25
28
  }
26
29
 
30
+ onChange(handler: (event: PubSubEvent, data: any) => void):IPubSubHandle {
31
+
32
+ return StateManager.dispatcher.on('state:changed', handler)
33
+ }
34
+
27
35
  getState(): ApplicationState {
28
36
  return this.appState || <ApplicationState>{}
29
37
  }
@@ -100,7 +108,7 @@ export class StateManager {
100
108
 
101
109
  registerStateByState(state: ApplicationState) {
102
110
  this.allStates[state.name] = state
103
- router.add(state.route, (context:any) => {
111
+ router.add(state.route, (context: any) => {
104
112
  if (this.setState(state.name, context)) {
105
113
 
106
114
  // @ts-ignore
package/.idea/aws.xml DELETED
@@ -1,17 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="accountSettings">
4
- <option name="activeProfile" value="profile:default" />
5
- <option name="activeRegion" value="us-east-1" />
6
- <option name="recentlyUsedProfiles">
7
- <list>
8
- <option value="profile:default" />
9
- </list>
10
- </option>
11
- <option name="recentlyUsedRegions">
12
- <list>
13
- <option value="us-east-1" />
14
- </list>
15
- </option>
16
- </component>
17
- </project>
package/.idea/modules.xml DELETED
@@ -1,8 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="ProjectModuleManager">
4
- <modules>
5
- <module fileurl="file://$PROJECT_DIR$/.idea/state-router.iml" filepath="$PROJECT_DIR$/.idea/state-router.iml" />
6
- </modules>
7
- </component>
8
- </project>
@@ -1,12 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <module type="WEB_MODULE" version="4">
3
- <component name="NewModuleRootManager">
4
- <content url="file://$MODULE_DIR$">
5
- <excludeFolder url="file://$MODULE_DIR$/temp" />
6
- <excludeFolder url="file://$MODULE_DIR$/.tmp" />
7
- <excludeFolder url="file://$MODULE_DIR$/tmp" />
8
- </content>
9
- <orderEntry type="inheritedJdk" />
10
- <orderEntry type="sourceFolder" forTests="false" />
11
- </component>
12
- </module>
package/.idea/vcs.xml DELETED
@@ -1,6 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="VcsDirectoryMappings">
4
- <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
- </component>
6
- </project>
package/pnpm-lock.yaml DELETED
@@ -1,23 +0,0 @@
1
- lockfileVersion: 5.3
2
-
3
- specifiers:
4
- '@dharmax/pubsub': ^1.0.1
5
- typescript: ^4.4.3
6
-
7
- dependencies:
8
- '@dharmax/pubsub': 1.0.1
9
-
10
- devDependencies:
11
- typescript: 4.4.4
12
-
13
- packages:
14
-
15
- /@dharmax/pubsub/1.0.1:
16
- resolution: {integrity: sha512-W4CLQBmZyg/oUe4nHw0yApvGwjqm7+in3Xt2J28HO0uDDfZIGtiEB7rbvGeaWfXiMBiB0ptvctzLn+CKMHykIQ==}
17
- dev: false
18
-
19
- /typescript/4.4.4:
20
- resolution: {integrity: sha512-DqGhF5IKoBl8WNf8C1gu8q0xZSInh9j1kJJMqT3a94w1JzVaBU4EXOSMrz9yDqMT0xt3selp83fuFMQ0uzv6qA==}
21
- engines: {node: '>=4.2.0'}
22
- hasBin: true
23
- dev: true