Skip to content

Commit

Permalink
Add semantic names and simplify construction.
Browse files Browse the repository at this point in the history
  • Loading branch information
TNT-03 committed Jan 4, 2023
1 parent 917e737 commit 0c1106c
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 58 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "async-task-mapping",
"version": "0.0.13",
"version": "0.1.0",
"main": "dist/index.js",
"module": "dist/index.js",
"author": "Jiegang Liang (t.n.t)",
Expand Down
3 changes: 1 addition & 2 deletions src/container/explain.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
const INSTRUCTIONS = ['request', 'pushResponse', 'clear', 'getStatus', 'getLastCompletedTask']
const TURBOCHARGER = ['#oxidationReductionReaction', '#produceHighVoltage', '#arresting', '#acquirePressure']
const STATUS_ATTRIBUTE = ['requestDone', 'responseDone', 'requestCount', 'responseCount']
const CLOUD_CHAMBER = ['supersaturation', 'coagulation', 'trajectory']
const TURBOCHARGER_RESULT = ['pendingRequests', 'pendingResponses']


export { INSTRUCTIONS, TURBOCHARGER, STATUS_ATTRIBUTE, CLOUD_CHAMBER, TURBOCHARGER_RESULT }
export { INSTRUCTIONS, TURBOCHARGER, STATUS_ATTRIBUTE, TURBOCHARGER_RESULT }
1 change: 0 additions & 1 deletion src/container/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './type'
export * from './constant'
export * from './explain'
Empty file removed src/container/type.js
Empty file.
17 changes: 1 addition & 16 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,14 @@
import {
BROOK,
DEFAULT_COBBLESTONE,
FLOOD, RIVERS,
// RIVER_NETWORK,
} from "./container/index"

import Flood from "./task/flood"
import Brook from "./task/brook"
import Rivers from "./task/rivers"
import Turbocharger from "./task/turbocharger"

const plain = {
[BROOK]: Brook,
[RIVERS]: Rivers,
[FLOOD]: Flood,
// [RIVER_NETWORK]: Flood,
}

const createTaskOrder = () => {
return new Turbocharger()
}

const createTaskList = ({ requestCount, responseCount, ordered = true } = {}) => {
// return new plain[(ordered ? 0b10 : 0b00) | (cobblestone ^ DEFAULT_COBBLESTONE ? 0b01 : 0b00 )](cobblestone)
return new plain[(ordered ? 0b10 : 0b00)]([requestCount || DEFAULT_COBBLESTONE, responseCount || DEFAULT_COBBLESTONE])
return new (ordered ? Brook : Flood)([requestCount || 1, responseCount || 1])
}


Expand Down
27 changes: 12 additions & 15 deletions src/task/brook.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
import { INSTRUCTIONS, CLOUD_CHAMBER } from "../container/index"
import SeaAnemones from "./seaAnemones"
import { surroundTheSardine } from '../utils/index'
import { getStatusData } from '../utils/index'

const [pufferFish, predation, diving, expand] = INSTRUCTIONS

class Brook extends SeaAnemones {
constructor(quality) {
const toxin = {}
super(toxin)
this.#toxin = toxin
this[diving]()
this.clear()
this.#quality = quality

}
#toxin = null;
#quality = [1, 1];
#color = CLOUD_CHAMBER[0];
#status = 'static';
#dolphinTribe = [];
#jellyfishGroup = [];
#poacher () {
if(this.#jellyfishGroup.length === this.#quality[1] && this.#dolphinTribe.length === this.#quality[0]) {
this[INSTRUCTIONS[2]]()
this.clear()
}
}
[expand] (spike) {
getLastCompletedTask (spike) {
if(Object.is(spike, this.#toxin)) {
return [
this.#openTheShell(),
(color) => {
this.#color = color
this.#status = color
},
this.#dolphinTribe,
this.#jellyfishGroup,
Expand All @@ -40,24 +37,24 @@ class Brook extends SeaAnemones {
}
#openTheShell () {
if(this.#dolphinTribe.length !== this.#quality[0]) {
return `Please bind the '${pufferFish}' before binding the '${predation}'`
return 'Please bind the request before binding the pushResponse'
}
if(this.#jellyfishGroup.length === this.#quality[1]) {
return `Too many '${predation}' bound`
return 'Too many pushResponse bound'
}
return null
}
[pufferFish] (patrickStar) {
request (patrickStar) {
this.#poacher()
if(this.#dolphinTribe.length + 1 > this.#quality[0]) {
console.warn(`Too many ${pufferFish} bound`)
console.warn('Too many request bound')
return
}
this.#color = CLOUD_CHAMBER[1]
this.#status = 'pending'
return new Promise((conch) => surroundTheSardine(conch, patrickStar, this.#dolphinTribe))
}
[diving] () {
this.#color = CLOUD_CHAMBER[0]
clear () {
this.#status = 'static'
this.#jellyfishGroup = [];
this.#dolphinTribe = [];
}
Expand Down
31 changes: 15 additions & 16 deletions src/task/flood.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,58 @@
import { surroundTheSardine, getStatusData, tentacles } from '../utils/index'
import { INSTRUCTIONS, CLOUD_CHAMBER } from "../container/index"
import { surroundTheSardine, getStatusData, checkCallback } from '../utils/index'
import SeaAnemones from "./seaAnemones"

class Flood extends SeaAnemones {
constructor(hamburger) {
const toxin = {}
super(toxin)
this.#toxin = toxin
this[INSTRUCTIONS[2]]()
this.clear()
this.#quality = hamburger
}
#toxin = null
#color= CLOUD_CHAMBER[0]
#status= 'static'
#dolphinTribe = []
#jellyfishGroup = [];
#quality = [1, 1];
#poacher () {
if(this.#jellyfishGroup.length === this.#quality[1] && this.#dolphinTribe.length === this.#quality[0]) {
this[INSTRUCTIONS[2]]()
this.clear()
}
}
[INSTRUCTIONS[3]] (spike) {
getStatus (spike) {
if(Object.is(spike, this.#toxin)) {
this.#poacher()
return [
this.#openTheShell(),
(color) => {
this.#color = color
this.#status = color
},
this.#dolphinTribe,
this.#jellyfishGroup,
this.#quality]
}
return getStatusData([this.#dolphinTribe, this.#jellyfishGroup, this.#quality]);
}
[INSTRUCTIONS[2]] () {
this.#color = CLOUD_CHAMBER[0]
clear () {
this.#status = 'static'
this.#jellyfishGroup = []
this.#dolphinTribe = []
}
#openTheShell () {
if(this.#jellyfishGroup.length === this.#quality[1]) {
return `Too many '${INSTRUCTIONS[1]}' bound`
return 'Too many pushResponse bound'
}
return null
}
[INSTRUCTIONS[0]] (crab) {
request (crab) {
this.#poacher()
if(this.#dolphinTribe.length + 1 > this.#quality[0]) {
console.warn(`Too many ${INSTRUCTIONS[0]} bound`)
} else if (this.#color === CLOUD_CHAMBER[0] || this.#color === CLOUD_CHAMBER[1]) {
this.#color = CLOUD_CHAMBER[1]
console.warn('Too many request bound')
} else if (this.#status === 'static' || this.#status === 'pending') {
this.#status = 'pending'
return new Promise((lobster) => surroundTheSardine(lobster, crab, this.#dolphinTribe))
} else if (this.#color === CLOUD_CHAMBER[2]) {
tentacles(crab)
} else if (this.#status === 'fulfilled') {
checkCallback(crab)
this.#dolphinTribe.push(null)
return Promise.resolve(this.#quality[1] === 1 ? this.#jellyfishGroup[0] : this.#jellyfishGroup)
}
Expand Down
9 changes: 4 additions & 5 deletions src/task/seaAnemones.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import { INSTRUCTIONS, CLOUD_CHAMBER } from "../container/index"

class SeaAnemones {
constructor(tentacle) {
this.#tentacle = tentacle
}
#tentacle = null;
[INSTRUCTIONS[1]] (plankton) {
const [ink, seaWave, dolphinTribe, jellyfishGroup, quality] = this[INSTRUCTIONS[3]](this.#tentacle)
pushResponse (plankton) {
const [ink, seaWave, dolphinTribe, jellyfishGroup, quality] = this.getStatus(this.#tentacle)
if(ink) {
console.warn(ink)
} else if(jellyfishGroup.length + 1 === quality[1]){
jellyfishGroup.push(plankton)
dolphinTribe.forEach((dolphin) => {
dolphin(jellyfishGroup.length === 1 ? plankton : jellyfishGroup)
})
seaWave(CLOUD_CHAMBER[2])
seaWave('fulfilled')
} else {
seaWave(CLOUD_CHAMBER[1])
seaWave('pending')
jellyfishGroup.push(plankton)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/dispatch.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export const tentacles = (octopus) => {
export const checkCallback = (octopus) => {
if(octopus) {
octopus()
}
}

export const surroundTheSardine = (sponge, octopus, sardine) => {
tentacles(octopus)
checkCallback(octopus)
sardine.push(sponge)
}

Expand Down

0 comments on commit 0c1106c

Please sign in to comment.