This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 將 Box 參數改為 funcion | |
| const Box = (f) => ({ | |
| map: (g) => Box((x) => g(f(x))), | |
| fold: (x) => f(x), | |
| // applicative | |
| ap: (other) => Box((x) => other.map(f(x)).fold()) | |
| }); | |
| Box.of = (value) => Box(() => value); | |
| const f1 = Box.of(10); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Task, Either } from "./types"; | |
| const { Left, Right } = Either; | |
| const fetchDB = (index) => | |
| Task((rej, res) => { | |
| res(index < 3 ? Left("not found") : Right("get data succeed")); | |
| }); | |
| // natural transformation: either to task | |
| const eitherToTask = (e) => e.fold(Task.rejected, Task.of); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // import { Machine } from 'xstate'; | |
| const states = { | |
| UNINITIALIZED: 'UNINITIALIZED', | |
| IDLE: 'IDLE', | |
| // PHONE | |
| PHONE_UNINITIALIZED: 'PHONE_UNINITIALIZED', | |
| PHONE_IDLE: 'PHONE_IDLE', | |
| PHONE_SET_FETCHING: 'PHONE_SET_FETCHING', | |
| PHONE_SET_SUCCEED: 'PHONE_SET_SUCCEED', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // you can copy type.js from https://github.com/FrontendMasters/hardcore-functional-js-v2/blob/master/types.js | |
| import { Task } from "./types"; | |
| // warp fetch | |
| const fetchRepo = (url) => | |
| Task((rej, res) => { | |
| fetch(url) | |
| .then((res) => res.json()) | |
| .then((content) => res(content)) | |
| .catch((e) => rej(e)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const Box = (x) => ({ | |
| map: f => Box(f(x)), // always return Box type. | |
| fold: (f) => f(x), // unwarp value | |
| }) | |
| const result= Box(10) | |
| .map((x) => x * 2) | |
| .map((x) => x + 10) | |
| .fold(x => x); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Either Monad | |
| const Left = x => ({ | |
| map: f => Left(x), | |
| fold: (f, g) => f(x) | |
| }); | |
| const Right = x => ({ | |
| map: f => Right(f(x)), | |
| fold: (f, g) => g(x) | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Emitter from 'eventemitter3'; | |
| import worker from './socket.worker'; | |
| let socket; | |
| export function getSocket() { | |
| if (!socket) socket = new WasmSocket(); | |
| return socket; | |
| } | |
| export const eventTypes = { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 原本綁定在 window object 的 wasm(rust) -> js function 參考, 因為放到 worker 改至 worker 執行環境的 self reference. | |
| // 將監聽函數轉發至 worker post message | |
| self.onSocketOpen = message => self.postMessage({ type: 'OPEN', message }); | |
| self.onSocketMessage = message => self.postMessage({ type: 'MESSAGE', message }); | |
| let wasm; | |
| console.log('worker execute'); | |
| import('./../../wasm/pkg/wasm').then(w => wasm = w); | |
| export const send = async message => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| test: /\.worker.js$/, | |
| exclude: [/node_modules|vendor/], | |
| use: [{ | |
| loader: 'workerize-loader', | |
| }], | |
| }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import crypto from 'crypto'; | |
| const key = Buffer.from('01234567012345670123456701234567', 'utf-8'); | |
| const iv = Buffer.from('0123456701234567', 'utf-8'); | |
| export const encrypt = message => { | |
| const cipher = crypto.createCipheriv('aes-256-cbc', key, iv); | |
| const encrypted = cipher.update(message, 'utf8', 'base64'); | |
| return encrypted + cipher.final('base64'); | |
| }; |
NewerOlder