Skip to content
This repository was archived by the owner on Oct 1, 2022. It is now read-only.

Commit 36c90f7

Browse files
authored
Merge pull request #192 from Pckool/develop
Various improvements to docs and minor patches to core
2 parents 3d0cf87 + 5484f42 commit 36c90f7

File tree

11 files changed

+72
-157
lines changed

11 files changed

+72
-157
lines changed

docs/.vuepress/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ module.exports = {
173173
'docs/state',
174174
'docs/collections',
175175
'docs/actions',
176-
'docs/controllers',
176+
// 'docs/controllers',
177177
'docs/core',
178178
'docs/route',
179179
'docs/persisting-data',

docs/.vuepress/styles/index.styl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,15 @@ $pulseYellow = #FFD696;
88
$pulseWhite = #F7F8F8;
99
$pulseLightBlue = #B6DCFF;
1010

11-
table code
12-
line-height: 2em !important;
11+
table
12+
tr, td, th
13+
border-color: $pulseGrey;
14+
tr:nth-child(2n)
15+
background-color: $pulseCodeBackground;
16+
17+
code
18+
line-height: 2em !important;
19+
1320

1421
pre.vue-container
1522
border-left-width: .5rem;

docs/.vuepress/theme/styles/code.styl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
padding 0.25rem 0.5rem
55
margin 0
66
font-size 0.85em
7-
background-color rgba(27,31,35,0.05)
7+
// background-color rgba(27,31,35,0.05)
8+
background-color $codeBgColor
89
border-radius 3px
910
.token
1011
&.deleted

docs/.vuepress/theme/styles/custom-blocks.styl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
$pulseCodeBackground = #282e3f;
12
.custom-block
23
.custom-block-title
34
font-weight 600
@@ -32,7 +33,7 @@
3233
border-radius 2px
3334
margin 1.6em 0
3435
padding 1.6em
35-
background-color #eee
36+
background-color $pulseCodeBackground
3637
h4
3738
margin-top 0
3839
figure, p

docs/v4/docs/controllers.md

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,15 @@ core.accounts.myAction();
2222

2323
The first parameter of the Controller function is `ControllerConfig`
2424

25-
```js
26-
const App = new Pulse();
27-
28-
const config = {
29-
collection: App.Collection()(),
30-
state: {
31-
MY_STATE: App.State(),
32-
MY_COMPUTED_STATE: App.Computed(() => true)
33-
}
25+
```ts
26+
import {collection, state} from '@pulsejs/core'
27+
28+
const accounts = {
29+
collection: collection(),
30+
MY_STATE: state(),
31+
MY_COMPUTED_STATE: state(() => true)
3432
};
35-
export const accounts = App.Controller(config);
33+
export const accounts;
3634
```
3735

3836
## Config Structure
@@ -60,7 +58,7 @@ For TypeScript users, the inferred types of the object you pass in will be prese
6058

6159
In some cases you will prefer to use more than the default Controller categories, you might want to spread actions to the root of the controller instance so they can be access like the following.
6260

63-
```js
61+
```ts
6462
accounts.myAction();
6563
```
6664

@@ -83,15 +81,13 @@ This is how a controller folder should be organized.
8381
### `index.ts`
8482

8583
```ts
86-
// import instance
87-
import App from '../../app';
8884
// import state
8985
import { state, computed, collection } from './state';
9086
// import actions, helpers and routes
9187
import * as actions from './actions';
9288

9389
// init controller, merge state and computed state
94-
const controller = App.Controller({ state: { ...state, ...computed }, collection }).root(actions);
90+
const controller = { state: { ...state, ...computed }, collection };
9591
```
9692

9793
The order of imports above is important, state/collections must be imported first to also allow them to be imported into `actions.ts` without creating a cyclic import. Sometimes this can cause `import * as ...` to return an empty object at runtime, following this structure will avoid that.

docs/v4/docs/core.md

Lines changed: 35 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -23,89 +23,36 @@ core.authentication.state.TOKEN.value;
2323
## Definition
2424

2525
```ts
26-
export const App = new Pulse();
26+
import { setCore } from '@pulsejs/core'
2727

2828
const core = {
2929
accounts,
3030
authentication
3131
};
3232

33-
export default App.Core(core);
33+
setCore(core); // register your core and initialize computed states
3434

35-
export type ICore = typeof core;
35+
export default core;
3636
```
3737

3838
Imports for `accounts` and `authentication` were ommited for this example.
3939

40-
The Pulse instance is created first as `App`, followed by an object that forms the root of the core object, in this case we're passing in two arbitrary Controllers.
41-
42-
Now we register the core with `App.Core()` which snapshots the core object. It can now be accessed anywhere with the very same function, without any parameters. (See [Usage]())
43-
44-
> _In practice the initilization of App should be in a seperate file (eg: `app.ts`) as it must occur before the imports that require the `App` instance and TSLint doesn't like code above imports._
40+
An object forms the root of the core object, in this case we're passing in two arbitrary Controllers.
4541

46-
See [Creating your core]() for the more detailed structure.
47-
48-
::: tip Why export the type?
49-
We're unable to directly import the core into controllers, as it would create cyclic dependencies which can cause horrible compile issues, especially at scale. This is why we use `App.Core()` to get the core inside controllers, but it still wouldn't be type safe.
50-
51-
However, TypeScript types are immune to this paradox and can time travel. :crystal_ball: Once you declare them, they are able to be refrenced in code before and after declaration. This means we can import just the type of the finalized core into our individual controllers.
52-
53-
Now when making changes to one Controller you'll see full intelisense in the other—regardless of the order the controllers are initialized.
54-
:::
55-
56-
## Usage
57-
58-
The core can be accessed from both outside and within itself, which means the syntax is slightly different for each. To demonstrate, we'll import and access a Controller named `accounts`.
59-
60-
> From **within** the core (this could be any file within)
61-
62-
```ts
63-
import { App } from './app'; // instance
64-
import { ICore } from './core'; // type from the future
65-
66-
const core = App.Core<ICore>();
67-
```
68-
69-
This method ensures this Controller can access other Controllers, even ones that might not be initialized yet. We import our time-traveling type `ICore` and assign it to the Core functions' generic.
70-
71-
> From **outside** the core
72-
73-
```js
74-
import core from './core';
75-
76-
core.accounts;
77-
```
78-
79-
It's safe to use the default import here as we know everything has been initialized, this would be the easiest way to access the core in your UI components.
42+
Now we register the core with `setCore()` which snapshots the core object.
8043

8144
### Caveats
8245

83-
#### 1) Destructuing imports
84-
85-
In an ideal world we'd be able to do this:
86-
87-
```ts
88-
const { accounts } = App.Core<ICore>();
89-
```
90-
91-
This would not work because at import-level accounts has not been defined yet, as assembly of the core happens last.
92-
93-
However if you import **without** destructuing, the constant you assign will be a direct reference to the core object within the App instance. So at runtime it will work.
94-
95-
```ts
96-
const core = App.Core<ICore>();
97-
```
98-
99-
#### 2) Using the core to supply initial state
46+
#### 1) Using the core to supply initial state
10047

10148
A way to remember this rule, is to only use `core.` notation inside functions that are not **immediately called**. Such as Computed functions and actions.
10249

103-
```js
104-
const core = App.Core<ICore>();
50+
```ts
51+
import core from './../core'
10552

10653
const state = {
107-
noworks: App.State(core.accounts.state.HELLO.value), // compile error
108-
works: App.Computed(() => {
54+
noworks: state(core.accounts.state.HELLO.value), // compile error
55+
works: state(() => {
10956
return core.accounts.state.HELLO.value // no compile error
11057
}),
11158
},
@@ -123,73 +70,52 @@ Create a folder in your application named **_core_**.
12370
In some cases you might want to create your core in a seperate repo or monorepo if you wish to use the same core in multiple projects.
12471
:::
12572

126-
### New File: `app.ts`
12773

128-
This is where you create an instance of Pulse.
129-
130-
```ts
131-
import React from 'react';
132-
import Pulse from 'pulse-framework';
133-
134-
export const App = new Pulse({
135-
framework: React
136-
});
137-
```
138-
139-
By this point your core should look something like this:
74+
At this point, your core should look something like this:
14075
::: vue
141-
├── **core**
76+
├── **/core**
14277
│ ├── **index.ts**
143-
│ ├── `app.ts`
14478
:::
14579

14680
> Leave index.ts empty for now.
14781

14882
### New Directory: `controllers`
14983

150-
Create a folder for your conrollers. Pulse advocates splitting up your core into modules using the [Controller]() class to containerize the module. However this step is optional, you're free to structure your core however you'd like.
84+
Create a folder for your conrollers. Pulse advocates splitting up your core into modules using an object; however, this step is optional. You're free to structure your core however you'd like.
15185

152-
> See [Controller]() documentation for more detail
86+
<!-- > See [Controller]() documentation for more detail -->
15387

15488
```ts
155-
import { App } from './app'; // instance
156-
import { ICore } from './core'; // type from the future
157-
158-
const core = App.Core<ICore>(); // grab sister controller
159-
160-
export const accounts = App.Controller({
161-
state: {
162-
IS_NEW_ACCOUNT: App.State().type(Boolean)
163-
}
164-
actions: {
165-
logout() {
166-
App.reset(core.authentication.state)
167-
}
168-
}
89+
import {state, action} from '@pulsejs/core';
90+
import core from './core'; // type from the future
91+
92+
export const accounts = {
93+
IS_NEW_ACCOUNT: state().type(Boolean),
94+
logout: action(() => {
95+
core.authentication.token.reset()
96+
})
97+
16998
});
99+
export default accounts;
170100
```
171101

172102
### New File: `core.ts`
173103

174104
```ts
175-
import { App } from './app';
176-
177105
import accounts from './controllers/accounts';
178106
import authentication from './controllers/authentication';
179107
180-
export const core = App.Core({
108+
export const core = setCore({
181109
accounts,
182110
authentication
183111
});
184112
185-
export type ICore = typeof core;
113+
export default core;
186114
```
187115

188116
Everything comes together in `core.ts`, it handles importing the Pulse instance, followed by your controllers.
189117

190-
`App.Core()` declares the final core structure and saves it to the instance so that subsequent calls.
191-
192-
Finally the core is registered and exported and `ICore` is exported as a type declaration.
118+
`setCore()` declares the final core structure and saves it to the instance so that subsequent calls.
193119

194120
### Export everything `index.ts`
195121

@@ -200,26 +126,21 @@ export default core;
200126

201127
## Structure at scale
202128

203-
Pulse is flexible, so you are free to do you own thing, but you must ensure that at the very least instance creation comes first, core construction comes last.
129+
Pulse is flexible, so you are free to do you own thing, but here is a core structure we recommend.
204130

205131
::: vue
206-
**core**
207-
├── .**index.ts**
208-
├── .**app.ts** _Create Pulse instance_
209-
│ ├── `controllers`
132+
**/core**
133+
├── **index.ts** _Export core and whatever else you want to expose to the application_
134+
├── **core.ts** _Construct the core_
135+
│ ├── `/controllers`
210136
│ │ └── **accounts**
211137
│ │ │ ├── **index.ts** _Create and export controller_
212138
│ │ │ ├── **state.ts** _Define all State, Computed State & a Collection_
213139
│ │ │ ├── **actions.ts** _All account actions as exported function_
214140
│ │ │ ├── **interfaces.ts** _Typescript interfaces for accounts_
215141
│ │ │ ├── **routes.ts** _api/socket endpoints for accounts_
216-
│ ├── `api`
217-
│ │ └── **index.ts**
218-
│ │ └── **rest.service.ts** _For rest api users_
219-
│ │ └── **socket.service.ts** _For websocket users_
220-
│ ├── `utils`
142+
│ ├── `/utils`
221143
│ │ └── **index.ts**
222-
│ ├── `data` _(Optional)_
223-
│ │ ├── **lists.json**
224-
└── .**core.ts** _Construct the core_
144+
│ ├── `/data` _(Optional)_
145+
│ │ ├── **lists.json**
225146
:::

0 commit comments

Comments
 (0)